<template>
	<view class="page" style="padding-bottom: 100rpx;" v-if="loading">
		<view class="advantage flex justify-between align-center">
			<view class="flex align-center">
				<u-avatar :src="userInfo.photo"></u-avatar>
				<view class="margin-left-xs">
					<view class="">
						{{userInfo.name}}
					</view>
					<view class="">{{userInfo.phone}}</view>
				</view>

			</view>
			<view class="">
				<text>{{member.name||''}}</text>
			</view>
		</view>
		<u-tabs :list="options" name="title" :is-scroll="false" :current="current" @change="change"></u-tabs>
		<view class="advantage">
			<view class="advantage-title">会员特权</view>
			<view class="advantage-item" v-for="(v,k) in interest" :key="k">
				<view class="advantage-item-icon">
					<image :src="v.image"></image>
				</view>
				<text>{{v.title}}</text>
			</view>
		</view>
		<view class="option">
			<view class="option-item" :class="curIndex==k?'border':''" v-for="(v, k) in fellowes" :key="k"
				@click="chooise(v,k)">
				<!-- <view class="flex align-center">
					<!-- <view class="round">
						<image class="img-lg round" :src="v.image" mode=""></image>
					</view> -->
				<view class="margin-left-xs flex justify-between align-center"
					style="width: 100%;box-sizing: border-box;">
					<view class="option-item-time">
						<text class="text-lg">{{v.title}}</text>

					</view>
					<view class="price text-red">
						￥{{v.price}}
					</view>
				</view>

				<!-- </view> -->
			</view>
		</view>
		<button class="cu-btn bg-blue pos round" style="width: 95%;" @click="InsertFellowInfo">立即支付</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current: 0,
				options: [

				],
				pageData: {
					pageIndex: 1,
					pageSize: 10,
					sorting: "CreationTime desc"
				},
				userInfo: uni.getStorageSync('userData').member,
				member: uni.getStorageSync('userData').data,
				interest: {},
				fellowes: [],
				curIndex: null,
				checkItem: {},
				loading:false
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			pay(v) {
				console.log(v)
			},
			change(e) {
				this.current = e
				this.interest = this.options[this.current].interest
				this.fellowes = this.options[this.current].fellowes
			},
			getData() {
				this.$ajax.GetFellows().then(res => {
					this.options = res.data
					this.interest = this.options[this.current].interest
					if(this.member.name){
						this.interest=this.options.filter((v,index)=>{
							if(this.member.name==v.title){
								this.current=index
								return v
							}
						})
					}
					this.fellowes = this.options[this.current].fellowes
					// this.options.map()
					this.loading=true
				})
			},
			chooise(item, index) {
				this.checkItem = item
				this.curIndex = index
			},
			// 购买会员信息
			InsertFellowInfo(row) {
				let that = this
				let data = {
					FellowId: that.checkItem.id,
					Level: that.options[that.current].level
				}
				let OpenId = uni.getStorageSync('userInfo').openId
				that.$ajax.InsertFellowInfo(data).then(res => {
					let data = {
						...JSON.parse(res.data),
						OpenId
					}
					that.$ajax.MiniProgramPay(data).then(res2 => {
						uni.requestPayment({
							provider: 'wxpay',
							...res2.data,
							success: function(res) {
								uni.navigateBack()
							},
						})
					})
				})


			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F4F4F4;
	}

	.page {
		.advantage {
			display: flex;
			flex-wrap: wrap;
			background: #fff;
			width: 700rpx;
			margin: 20rpx auto;
			border-radius: 12rpx;
			box-sizing: border-box;
			padding: 15rpx;

			.advantage-title {
				width: 700rpx;
				text-align: center;
				font-size: 18px;
				font-weight: bold;
				color: #666;
			}

			.advantage-item {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 330rpx;
				height: 170rpx;

				.advantage-item-icon {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 90rpx;
					height: 90rpx;
					border-radius: 50%;
					border: 1px solid #f5984a;
					margin-bottom: 10rpx;

					image {
						width: 60rpx;
						height: 60rpx;
					}
				}

				text {
					font-size: 14px;
					color: #444;
				}
			}
		}

		.pos {
			position: fixed;
			bottom: 30rpx;
			left: 50%;
			transform: translateX(-50%);
		}

		.option {
			width: 700rpx;
			margin: auto;

			.option-item {
				display: flex;
				align-items: center;
				box-sizing: border-box;
				justify-content: space-between;
				width: 700rpx;
				background: #fff;
				border-radius: 12rpx;
				box-sizing: border-box;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.option-item-button {
					background: #f5984a;
					height: 70rpx;
					width: 160rpx;
					border-radius: 40rpx;
					text-align: center;
					line-height: 70rpx;
					color: #fff;
					font-size: 16px;

					&:active {
						background: #ca7c3d;
					}
				}

				.option-item-time {
					margin-bottom: 6rpx;

					text {
						&:nth-child(1) {
							font-weight: bold;
						}

						&:nth-child(2) {
							font-size: 14px;
							color: #777;
						}
					}
				}

				.option-item-price {
					position: relative;

					text {
						&:nth-child(1) {
							font-size: 14px;
							color: #777;
						}

						&:nth-child(2) {
							font-size: 24px;
							color: #f5984a;
						}
					}

					.option-item-price-line {
						position: absolute;
						left: 0;
						bottom: 10px;
						width: 32px;
						height: 1px;
						background: #999;
					}
				}
			}
		}

	}

	.border {
		border: 1px solid #f29100;
	}
</style>